<?php

namespace app\seller\controller;

use app\common\controller\Seller;

/**
 * 商品评价表
 *
 * @icon fa fa-circle-o
 */
class Goodscomment extends Seller
{
 
	protected $relationSearch = true;
	
    /**
     * GoodsComment模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('GoodsComment');
        $this->view->assign("scoreList", $this->model->getScoreList());
        $this->view->assign("isAnonymousList", $this->model->getIsAnonymousList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Seller.php中对应的方法复制到当前控制器,然后进行修改
     */
	
	/**
	 * 查看
	 */
	public function index()
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		if ($this->request->isAjax())
		{
			//如果发送的来源是Selectpage，则转发到Selectpage
			if ($this->request->request('pkey_name'))
			{
				return $this->selectpage();
			}
			list($where, $sort, $order, $offset, $limit) = $this->buildparams();
			$total = $this->model
				->with('goods,order')
				->where($where)
				->where(['goods.business_id' => $this->auth->id])
				->order($sort, $order)
				->count();
			
			$list = $this->model
				->with('goods,order')
				->where($where)
				->where(['goods.business_id' => $this->auth->id])
				->order($sort, $order)
				->limit($offset, $limit)
				->select();
			
			$list = collection($list)->toArray();
			$result = array("total" => $total, "rows" => $list);
			
			return json($result);
		}
		return $this->view->fetch();
	}
	
	/**
	 * 编辑
	 */
	public function edit($ids = NULL)
	{
		$row = $this->model->alias('c')
			->join('goods g', 'c.goods_id = g.id', 'left')
			->where(['c.id' => $ids, 'g.business_id' => $this->auth->id])
			->find();
		if (!$row)
			$this->error(__('No Results were found'));
		$adminIds = $this->getDataLimitAdminIds();
		if (is_array($adminIds))
		{
			if (!in_array($row[$this->dataLimitField], $adminIds))
			{
				$this->error(__('You have no permission'));
			}
		}
		if ($this->request->isPost())
		{
			$params = $this->request->post("row/a");
			if ($params)
			{
				try
				{
					//是否采用模型验证
					if ($this->modelValidate)
					{
						$name = basename(str_replace('\\', '/', get_class($this->model)));
						$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
						$row->validate($validate);
					}
					$result = $this->model->save([
						'reply' => $params['reply'],
						'reply_time' => time()
					],['id' => $ids]);
					if ($result !== false)
					{
						$this->success();
					}
					else
					{
						$this->error($row->getError());
					}
				}
				catch (\think\exception\PDOException $e)
				{
					$this->error($e->getMessage());
				}
			}
			$this->error(__('Parameter %s can not be empty', ''));
		}
		$this->view->assign("row", $row);
		return $this->view->fetch();
	}
}
